package com.quality.web.controller.business;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.quality.business.domain.QualityGroup;
import com.quality.business.service.QualityGroupService;
import com.quality.common.core.controller.BaseController;
import com.quality.common.core.domain.AjaxResult;
import com.quality.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author aben
 * @date2022/6/26 22:15
 */
@RestController
@RequestMapping("/quality/group")
public class QualityGroupController extends BaseController {

    @Autowired
    private QualityGroupService groupService;

    @GetMapping("/list")
    public TableDataInfo list(String groupName){
        startPage();
        List<QualityGroup> list = groupService.lambdaQuery().like(QualityGroup::getGroupName, groupName).list();
        return getDataTable(list);
    }


    @GetMapping("/byId")
    public AjaxResult byId(Integer id){
        return AjaxResult.success(groupService.getById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody QualityGroup group){
        errIf(StrUtil.isEmpty(group.getGroupName()),"分组名称不能为空");
        groupService.save(group);
        return AjaxResult.success();
    }


    @PostMapping("/update")
    public AjaxResult update(@RequestBody QualityGroup group){
        errIf(StrUtil.isEmpty(group.getGroupName()),"分组名称不能为空");
        groupService.updateById(group);
        return AjaxResult.success();
    }

    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody JSONObject object){
        List<Integer> ids = object.getList("ids", Integer.class);
        errIf(CollectionUtil.isEmpty(ids),"请选择数据");
        groupService.removeByIds(ids);
        return AjaxResult.success();
    }
}